package com.yjh.admin.config.data;


import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Slf4j
public class DynamicDataSourceAspect {

    @Before("@annotation(ds)")
    public void changeDataSource(JoinPoint point, DataSource ds) throws Throwable {
        log.info("Before DataSource : " + ds.value() + " > " + point.getSignature());
        DataSourceContext.setDataSource(ds.value());//切换数据源
    }

    @After("@annotation(ds)")
    public void restoreDataSource(JoinPoint point, DataSource ds) {
        log.info("After DataSource : " + ds.value() + " > " + point.getSignature());
        DataSourceContext.clearDataSource();//清理数据源
    }
}

